package org.shangrila.app.sys.dao;

import java.util.List;

import org.shangrila.app.sys.eo.SysMenus;
import org.shangrila.app.sys.eo.SysOrganization;
import org.shangrila.app.sys.eo.SysUser;
import org.shangrila.comm.dao.DbAccess;

public class SysDao {
	
	public DbAccess dbAccess = new DbAccess();
	
	public static final String SysUser_findAll="SELECT s FROM SysUser s";
	public static final String SysUser_findByUserno="SELECT s FROM SysUser s WHERE s.userno=?1";
	public static final String SysUser_findByEmail="SELECT s FROM SysUser s WHERE s.email=?1";
	public static final String SysUser_findByUsernoPassword="SELECT s FROM SysUser s WHERE s.userno=?1 and s.password=?2";
	public static final String SysUser_findByOrgID="SELECT s FROM SysUser s WHERE s.sysOrganization.id=?1";

	public static final String SysMenus_findAll="SELECT s FROM SysMenus s ORDER BY s.orderby";
	public static final String SysMenus_findMenus="SELECT s FROM SysMenus s WHERE s.id=s.pid ORDER BY s.orderby";
	public static final String SysMenus_findMenuItems="SELECT s FROM SysMenus s WHERE s.id<>s.pid ORDER BY s.orderby";
	public static final String SysMenus_findByPid="SELECT s FROM SysMenus s WHERE s.pid=?1 ORDER BY s.orderby";
	
	public static final String SysOrganization_findAll="SELECT s FROM SysOrganization s ORDER BY s.pid,s.id";
	public static final String SysOrganization_findByType="SELECT s FROM SysOrganization s WHERE s.type=?1";
	
	/**
	 * 根据微信号获取用户
	 */
	public static final String SysUser_findByWxno="SELECT s FROM SysUser s WHERE s.wxno=?1";

	
	//find SysUser
	@SuppressWarnings("unchecked")
	public List<SysUser> findSysUserAll() {
		return (List<SysUser>) dbAccess.createQuery(SysUser_findAll).getResultList();
	}
	@SuppressWarnings("unchecked")
	public List<SysUser> findSysUserByOrgID(long orgid) {
		return (List<SysUser>) dbAccess.createQuery(SysUser_findByOrgID).setParameter(1, orgid).getResultList();
	}

	public SysUser findSysUserByUserno(String userno) {
		return (SysUser) dbAccess.createQuery(SysUser_findByUserno).setParameter(1, userno).getSingleResult();
	}
	public SysUser findSysUserByEmail(String email) {
		return (SysUser) dbAccess.createQuery(SysUser_findByEmail).setParameter(1, email).getSingleResult();
	}
	public SysUser findSysUserByUsernoPassword(String userno,String password) {
		return (SysUser) dbAccess.createQuery(SysUser_findByUsernoPassword).setParameter(1, userno).setParameter(2, password).getSingleResult();
	}
	
	/**
	 * 根据微信Userid获取用户
	 * @return
	 */
	public SysUser findSysUserByWxno(String wxno) {
		return (SysUser) dbAccess.createQuery(SysUser_findByWxno).setParameter(1, wxno).getSingleResult();
	}

	//find SysMenus
	@SuppressWarnings("unchecked")
	public List<SysMenus> findSysMenusAll() {
		return (List<SysMenus>) dbAccess.createQuery(SysMenus_findAll).getResultList();
	}
	@SuppressWarnings("unchecked")
	public List<SysMenus> findSysMenus() {
		return (List<SysMenus>) dbAccess.createQuery(SysMenus_findMenus).getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<SysMenus> findSysMenuItems() {
		return (List<SysMenus>) dbAccess.createQuery(SysMenus_findMenuItems).getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<SysMenus> findSysMenusByPid(long pid) {
		return (List<SysMenus>) dbAccess.createQuery(SysMenus_findByPid).setParameter(1, pid).getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<SysOrganization> findSysOrganizationAll() {
		return (List<SysOrganization>) dbAccess.createQuery(SysOrganization_findAll).getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<SysOrganization> findSysOrganizationByType(String ptype) {
		return (List<SysOrganization>) dbAccess.createQuery(SysOrganization_findByType).setParameter(1, ptype).getResultList();
	}
	
}
